//
//  ScheduleHistoryView.m
//  MarryBackEnd
//
//  Created by mac on 2020/12/4.
//  Copyright © 2020 WPP. All rights reserved.
//

#import "ScheduleHistoryView.h"
#import "ScheduleHistoryModel.h"
@interface ScheduleHistoryView ()
@property(nonatomic,strong)UIImageView *leftIV;
@property(nonatomic,strong)UILabel *leftLab;
@property(nonatomic,strong)NSMutableArray<UIView *> *itemsArray;
@property(nonatomic,strong)NSMutableArray<UIView *> *itemsArray1;
@end

@implementation ScheduleHistoryView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = WhiteColor;
        self.layer.cornerRadius = 6*ADAPTER_WIDTH;
        self.layer.masksToBounds = YES;
    }
    return self;
}
- (void)setHistoryModel:(ScheduleHistoryModel *)historyModel
{
    _historyModel = historyModel;
    if (historyModel.result == 0) {
        self.height = 0;
    }
    [self leftIV];
    self.leftLab.text = @"历史上的今天";
    CGFloat orinY = self.leftIV.bottom + 18*ADAPTER_WIDTH;
    //[self.itemsArray removeAllObjectsAndViews];
    [self.itemsArray enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray removeAllObjects];
    for (NSInteger index = 0; index < historyModel.result.count; index ++) {
        ScheduleHistoryResultModel *resultModel = historyModel.result[index];
        resultModel.title = [NSString stringWithFormat:@"%ld年%ld月%ld日 %@",resultModel.year,resultModel.month,resultModel.day,resultModel.title];
        UIView *pointView = [self createPointView];
        pointView.top = orinY;
        
        UILabel *titleLab = [self createDesLabWithDes:resultModel.title];
        titleLab.top = orinY - 5*ADAPTER_WIDTH;
        
        orinY = titleLab.bottom + 18*ADAPTER_WIDTH;
        
        [self.itemsArray addObject:pointView];
        [self.itemsArray addObject:titleLab];
        if (index > 0 && index < historyModel.result.count) {
            //[self createLine:pointView];
        }
        [self.itemsArray1 addObject:pointView];
        if (index == 4) {
            break;
        }
    }
    self.height = orinY;
    !self.refreshUIBlock ?: self.refreshUIBlock ();
}
- (UIView *)createPointView
{
    UIView *pointView = [[UIView alloc]init];
    [self addSubview:pointView];
    pointView.backgroundColor = rgba(229, 229, 229, 1);
    pointView.layer.cornerRadius = 4*ADAPTER_WIDTH;
    pointView.layer.masksToBounds = YES;
    pointView.frame = CGRectMake(14*ADAPTER_WIDTH, 13*ADAPTER_WIDTH, 8*ADAPTER_WIDTH, 8*ADAPTER_WIDTH);
    return pointView;
}
- (UILabel *)createDesLabWithDes:(NSString *)title
{
    UILabel *lab = [[UILabel alloc]init];
    [self addSubview:lab];
    lab.font = kFontRegular13;
    lab.textColor = rgba(51, 51, 51, 1);
    lab.numberOfLines = 0;
    lab.text = title;
    CGFloat titleHeight = [title sizeWithMaxWidth:self.width - 48*ADAPTER_WIDTH andFont:kFontRegular13].height;
    lab.frame = CGRectMake(28*ADAPTER_WIDTH, 0, self.width - 48*ADAPTER_WIDTH, titleHeight);
    return lab;
}
- (void)createLine:(UIView *)currentPointView
{
    UIView *prePointView = self.itemsArray1.lastObject;
    if (prePointView) {
        CGFloat orinY = prePointView.centerY;
        for (NSInteger index = 0; index < 100; index ++) {
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = rgba(229, 229, 229, 1);
            lineView.frame = CGRectMake(0, 0, 1, 3*ADAPTER_WIDTH);
            [self addSubview:lineView];
            lineView.centerX = prePointView.centerX;
            lineView.top = orinY;
            orinY = lineView.bottom + 1.5*ADAPTER_WIDTH;
            if ((orinY + 4.5*ADAPTER_WIDTH) > currentPointView.bottom) {
                break;
            }
            [self.itemsArray addObject:lineView];
        }
    }
}
#pragma mark - lazy懒加载
- (UIImageView *)leftIV
{
    if (!_leftIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"ScheduleManager_time"];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        iv.frame = CGRectMake(10*ADAPTER_WIDTH, 11*ADAPTER_WIDTH, 16*ADAPTER_WIDTH, 17*ADAPTER_WIDTH);
        _leftIV = iv;
    }
    return _leftIV;
}
- (UILabel *)leftLab
{
    if (!_leftLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(51, 51, 51, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(30*ADAPTER_WIDTH, 0, 200*ADAPTER_WIDTH, 15*ADAPTER_WIDTH);
        lab.centerY = self.leftIV.centerY;
        _leftLab = lab;
    }
    return _leftLab;
}
- (NSMutableArray<UIView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (NSMutableArray<UIView *> *)itemsArray1
{
    if (!_itemsArray1) {
        _itemsArray1 = [NSMutableArray array];
    }
    return _itemsArray1;
}
@end
